﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GetTogether.Studio.Logging;

public partial class logging_Callback_LoggingList : GetTogether.Studio.Web.UI.PageCallback
{
    bool SizeNoZero
    {
        get
        {
            return GetTogether.Utility.NumberHelper.ToInt(Request["size-no-zero"], 0) == 1;
        }
    }
    public string ProjectName
    {
        get
        {
            return Request["pn"];
        }
    }
    //public string[] LogsUrl = new string[] { "http://10.100.106.47:10001/logs/Adholidays/WTL/B2B(Soap)/2013-05-07/" };
    public string[] LogsUrl = new string[] { "http://127.0.0.1/logs/" };
    string ItemTemplate = @"{name:'[Name]',url:[Url],title:'[Title]',code:'[Code]',isParent:[IsParent],hasRead:0}";
    string UrlParameterTemplate = @"'javascript:GetPage(\'{0}\');'";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request["path"]))
        {
            LogsUrl = new string[] { Request["path"] };
        }
        switch (type)
        {
            case 1:
                System.Text.StringBuilder sbTree = new System.Text.StringBuilder();
                string header = @"{name:'Logging List',url:null,title:'Logging List',code:'Logging List',isParent:1,hasRead:1}";
                sbTree.Append(header);

                foreach (string url in LogsUrl)
                {
                    if (sbTree.Length > 0) sbTree.Append(",");
                    sbTree.Append(ItemTemplate.Replace("[Name]", url).Replace("[Title]", url).Replace("[Code]", url).Replace("[IsParent]", "1")).Replace("[Url]", "null");
                }
                sbTree.Insert(0, "[").Append("]");
                Response.Write(sbTree.ToString());
                //Response.Write(@"[{name:'Fare List',url:'javascript:GetLink(\'\',null,null,null,null,null,null,null,null,null,null,true);',title:'Fare List',code:'Fare List',isParent:1,hasRead:1},{name:'AD Fare',url:'javascript:GetLink(\'AD Fare\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'AD Fare',code:'AD Fare',isParent:1,hasRead:0},{name:'BJ Fare',url:'javascript:GetLink(\'BJ Fare\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'BJ Fare',code:'BJ Fare',isParent:1,hasRead:0},{name:'GV',url:'javascript:GetLink(\'GV\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'GV',code:'GV',isParent:1,hasRead:0},{name:'GZ Fare',url:'javascript:GetLink(\'GZ Fare\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'GZ Fare',code:'GZ Fare',isParent:1,hasRead:0},{name:'GZ News',url:'javascript:GetLink(\'GZ News\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'GZ News',code:'GZ News',isParent:1,hasRead:0},{name:'HK Fare',url:'javascript:GetLink(\'HK Fare\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'HK Fare',code:'HK Fare',isParent:1,hasRead:0},{name:'HK Fare - Wholesales',url:'javascript:GetLink(\'HK Fare - Wholesales\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'HK Fare - Wholesales',code:'HK Fare - Wholesales',isParent:1,hasRead:0},{name:'HK News',url:'javascript:GetLink(\'HK News\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'HK News',code:'HK News',isParent:1,hasRead:0},{name:'HK Other Product',url:'javascript:GetLink(\'HK Other Product\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'HK Other Product',code:'HK Other Product',isParent:1,hasRead:0},{name:'Hotel Pack',url:'javascript:GetLink(\'Hotel Pack\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'Hotel Pack',code:'Hotel Pack',isParent:1,hasRead:0},{name:'HPD',url:'javascript:GetLink(\'HPD\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'HPD',code:'HPD',isParent:1,hasRead:0},{name:'SH Fare',url:'javascript:GetLink(\'SH Fare\',\'\',\'\',\'Y\',1,null,null,null,null,null,null,true);',title:'SH Fare',code:'SH Fare',isParent:1,hasRead:0}]");
                break;
            case 2:
                //Response.Write(@"[{name:'CX-GV',url:'javascript:GetLink(\'GV\\\\CX-GV\',null,null,null,null,null,null,null,null,null,null,true);',title:'GV\\CX-GV',code:'GV\\CX-GV',isParent:0,hasRead:0},{name:'KA-GV',url:'javascript:GetLink(\'GV\\\\KA-GV\',null,null,null,null,null,null,null,null,null,null,true);',title:'GV\\KA-GV',code:'GV\\KA-GV',isParent:0,hasRead:0}]");
                string folder = Request["folder"];
                System.Text.StringBuilder sbTreeSub = new System.Text.StringBuilder();
                if (folder != "Logging List")
                {
                    Loggings logs = Util.GetLoggings(folder);
                    if (logs != null && logs.Count > 0)
                    {
                        string sortColumn = "Name";
                        bool isAsc = true;
                        if (logs[0].IsFile)
                        {
                            sortColumn = "Date";
                            isAsc = false;
                        }
                        logs.SortBy(sortColumn, isAsc);
                        foreach (Loggings.Logging lg in logs)
                        {
                            string buttonClass = "a-btn2";
                            if (SizeNoZero && lg.Size == "0") continue;
                            //if (lg.IsFile && lg.Date.Date != DateTime.Now.Date) continue;
                            string code = string.Concat(folder, folder.EndsWith("/") ? "" : "/", lg.Name);
                            string url = "null";
                            //if (lg.IsFile) url = string.Format(UrlParameterTemplate, code);
                            if (sbTreeSub.Length > 0) sbTreeSub.Append(",");
                            string color = "gray";
                            if (lg.Date.Date == DateTime.Now.Date && lg.Date.Hour == DateTime.Now.Hour && (DateTime.Now.Minute - lg.Date.Minute) <= 2)
                            {
                                color = "blue";
                                buttonClass = "a-btn";
                            }
                            string name = string.Concat(lg.Name, @"<span style=\'color:gray;\'> - (Date:", lg.Date.ToString("yyyy-MM-dd"), @"<label style=\'color:", color, @"\'>", lg.Date.ToString(" HH:mm"), "</label>", lg.IsFile ? string.Concat(", Size:", lg.Size) : "", ")</span>");
                            string links = "";
                            if (lg.IsFile)
                            {
                                links = string.Format(@" - <a class=\'{0}\' href=\'javascript:window.open(""{1}"");void 0;\'>Open</a>", buttonClass, code);
                                links += string.Format(@"<span class=\'mm-split\'>|</span> <a class=\'{0}\' href=\'javascript:GetPage(""{1}"",0);void 0;\'>Text</a>", buttonClass, code);
                                links += string.Format(@"<span class=\'mm-split\'>|</span> <a class=\'{0}\' href=\'javascript:GetPage(""{1}"",1);void 0;\'>Soap</a>", buttonClass, code);
                                name += links;
                            }
                            else
                            {
                                if (logs.Count > 1)
                                {
                                    links = string.Format(@" - <a class=\'a-btn\' href=\'javascript:window.open(""LoggingList.aspx?pn={0}&path={1}"");void 0;\'>Open in New Window</a>", ProjectName, code);
                                    name += links;
                                }
                            }
                            sbTreeSub.Append(ItemTemplate.Replace("[Name]", name).Replace("[Title]", name).Replace("[Code]", code).Replace("[IsParent]", lg.IsFile ? "0" : "1")).Replace("[Url]", url);
                        }
                    }
                }
                sbTreeSub.Insert(0, "[").Append("]");
                Response.Write(sbTreeSub.ToString());
                break;
            default:
                break;
        }
    }
}